package program.atm.data;

import javax.swing.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ATM机数据.
 * 存储所有账号信息.
 *
 * @author 林志鹏
 * @version 1.0.0
 * @date 2020/11/8 15:21
 */
public class ATMData {

    /**
     * 所有账号数据.
     * 键 账号
     * 值 账号数据
     */
    static Map<String, CustomerAccount> allAccount;

    /**
     * 初始化数据.
     */
    static {
        //初始化所有账户信息
        allAccount = new HashMap<>(8);
        allAccount.put("6228481698729891", new CustomerAccount("6228481698729891", "123456", 10000));
        allAccount.put("1111111111111111", new CustomerAccount("1111111111111111", "123456", 10000));
        allAccount.put("2222222222222222", new CustomerAccount("2222222222222222", "123456", 10000));
        allAccount.put("3333333333333333", new CustomerAccount("3333333333333333", "123456", 10000));
    }

    /**
     * 登录.
     *
     * @param number
     * @param pwd
     * @return
     */
    public static CustomerAccount loginATM(String number, String pwd) {
        //是否存在账号
        if (allAccount.containsKey(number)) {
            CustomerAccount account = allAccount.get(number);

            //判断账号是否被锁定
            if (account.getErrorCount() >= 3) {
                JOptionPane.showMessageDialog(null, "登录失败！该账号疑似已被锁定", "提示消息", JOptionPane.WARNING_MESSAGE);
                return null;
                //密码是否一致
            } else if (account.getPassword().equals(pwd)) {
                JOptionPane.showMessageDialog(null, "登录成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
                //重置错误次数
                account.setErrorCount(0);
                return account;
            }
            account.setErrorCount(account.getErrorCount() + 1);
            JOptionPane.showMessageDialog(null, "密码错误，剩余重试次数【" + (3 - account.getErrorCount()) + "】", "提示消息", JOptionPane.WARNING_MESSAGE);
            if (account.getErrorCount() >= 3) {
                account.setStatus(1);
            }
        } else {
            JOptionPane.showMessageDialog(null, "登录失败！卡号或密码错误", "提示消息", JOptionPane.WARNING_MESSAGE);
        }
        return null;
    }

    /**
     * 转账.
     * 通过登录的用户向指定的银行账号（在系统中已经保存）转账，若银行卡不存在，
     * 则提示银行卡号输入错误，转账成功则提示“转账成功”
     *
     * @param account
     * @param targetAccount
     * @param money
     * @return
     */
    public static boolean transfer(CustomerAccount account, String targetAccount, int money) {

        if (!allAccount.containsKey(targetAccount)) {
            JOptionPane.showMessageDialog(null, "银行卡号输入错误", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if (account.getMoney() < money) {
            JOptionPane.showMessageDialog(null, "转账失败，余额不足", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        //添加目标账号金额
        CustomerAccount targetAccountData = allAccount.get(targetAccount);
        targetAccountData.setMoney(targetAccountData.getMoney() + money);
        //修改剩余金额
        account.setMoney(account.getMoney() - money);
        //日期格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //添加交易记录
        account.getOperationRecod().add("【" + LocalDateTime.now().format(dateTimeFormatter) + "】 ATM 转账 [" + money + "]元 给账号[" + targetAccount + "] 当前余额[" + account.getMoney() + "]元");
        targetAccountData.getOperationRecod().add("【" + LocalDateTime.now().format(dateTimeFormatter) + "】 接收到ATM账号 [" + account.getNumber() + "] 的转账 [" + money + "]元  当前余额[" + targetAccountData.getMoney() + "]元");
        JOptionPane.showMessageDialog(null, "转账成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
        return true;
    }

    /**
     * 查询所有账户信息.
     *
     * @return
     */
    public static List<CustomerAccount> getAllAcount() {
        //接收所有结果
        List<CustomerAccount> all = new ArrayList<>();
        //遍历集合
        Set<String> keys = allAccount.keySet();
        for (String key : keys) {
            CustomerAccount account = allAccount.get(key);
            all.add(account);
        }
        return all;
    }
}
